package backend.cms.controller;
import java.util.List;
import backend.cms.manager.CmsManager;
import backend.paging.Paging;
import core.cms.model.Block;
import com.opensymphony.xwork2.ActionSupport;

public class CmsController extends ActionSupport{

	/**
	 * 
	 */
	private static final long serialVersionUID = 14343L;
	private String identifier ;
	private String content ;
	private Block block ;
	private Block block1 ;
	private CmsManager cmsmanager ;
	private String msg ;
	private String errorMsg ;
	private int pageSize ; // default value 10
	private int p=1 ;
	private int totalPage ;
	private List<String> pageHtml ;
	private List<Block> blocks;
	 
	List<Block> lstblock = null;

	public CmsController()
	{
		cmsmanager =  new CmsManager();
	}
	public String execute() {
		this.identifier=this.getIdentifier();
		
		this.content=this.getContent();
		block1 = cmsmanager.getBlockByIdentifier(block.getIdentifier());
		this.block = this.getBlock();
		if(block1==null & this.block.getIdentifier()!=null) {
		cmsmanager.addBlock(block);
		this.setMsg("Content saved Successfully");
		this.lstblock = cmsmanager.getCmsList();
		}
		else {
			//cmsmanager.updateBlock(block.getContent(),block.getIdentifier());
			this.setErrorMsg("Could not save content. Identifier Allready Exists ");
			this.lstblock = cmsmanager.getCmsList();
		}
		return SUCCESS ;
	}
	
	public String UpdateCms()
	{
		this.identifier=this.getIdentifier();		
		this.content=this.getContent();
		block1 = cmsmanager.getBlockByIdentifier(block.getIdentifier());
		this.block = this.getBlock();
		
		
		if(block1==null & this.block.getIdentifier()!=null) {
		cmsmanager.addBlock(block);
		this.setMsg("Content saved Successfully");
		this.lstblock = cmsmanager.getCmsList();
		}
		else {
			cmsmanager.updateBlock(this.block.getContent(),this.block.getIdentifier(),this.block1.getUuid());
			this.setErrorMsg("Content is updated Successfully");
			this.lstblock = cmsmanager.getCmsList();
		}
		
		return SUCCESS ;
	}
	
	public String deleteBlock()
	{
		this.identifier=this.getIdentifier();		
		this.content=this.getContent();
		block1 = cmsmanager.getBlockByIdentifier(this.getIdentifier());
		cmsmanager.deleteBlockContent(block1);
		this.lstblock = cmsmanager.getCmsList();
		this.setMsg("Content deleted Successfully");
		return SUCCESS ;
		
		
	}
	
	public String getCmsList()
	
	{
		List<Block> cmsFullList = cmsmanager.getCmsList();
		if(this.pageSize == 0)
		{
			this.setPageSize(10);
		}
		Paging<Block> paging = new Paging<Block>();
		this.setPageHtml(paging.getPageHtml(this.getP(), pageSize, cmsFullList.size()));
		this.setBlocks(paging.getPageList(this.pageSize,cmsFullList , this.p));
		
		return SUCCESS;
	}
	
	public List<Block> getLstblock() {
		return lstblock;
	}
	public void setLstblock(List<Block> lstblock) {
		this.lstblock = lstblock;
	}
	public Block getBlock() {
		return block;
	}
	public void setBlock(Block block) {
		this.block = block;
	}
	public String getIdentifier() {
		return identifier;
	}
	public void setIdentifier(String identifier) {
		this.identifier = identifier;
	}
	public String getContent() {
		return content;
	}
	public void setContent(String content) {
		this.content = content;
	}
	public String getMsg() {
		return msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}
	public String getErrorMsg() {
		return errorMsg;
	}
	public void setErrorMsg(String errorMsg) {
		this.errorMsg = errorMsg;
	}
	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public int getP() {
		return p;
	}
	public void setP(int p) {
		this.p = p;
	}
	public int getTotalPage() {
		return totalPage;
	}
	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}
	public List<String> getPageHtml() {
		return pageHtml;
	}
	public void setPageHtml(List<String> pageHtml) {
		this.pageHtml = pageHtml;
	}
	public List<Block> getBlocks() {
		return blocks;
	}
	public void setBlocks(List<Block> blocks) {
		this.blocks = blocks;
	}
	public String quote(String data)
	{
		String content = new String("");
		System.out.println(data + "this is done");
		Block blo =  cmsmanager.getBlockByIdentifier(data) ;
		if(blo != null)
		{
			content = blo.getContent();
		}
		return content ;
	}
}
